package com.qys.livesMall.shop.service.impl;

import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.shop.dao.H2ShopAdvertInfoDao;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.modal.dto.shop.advert.AddAdvert;
import com.qys.livesMall.shop.modal.dto.shop.advert.EditAdvert;
import com.qys.livesMall.shop.modal.entity.H2ShopAdvertInfo;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.vo.ShopAdvertInfoVO;
import com.qys.livesMall.shop.service.H2ShopAdvertInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 店铺轮播广告信息 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-03
 */
@Service
public class H2ShopAdvertInfoServiceImpl  implements H2ShopAdvertInfoService {

    @Autowired
    private H2ShopBaseDao h2ShopBaseDao;
    @Autowired
    private H2ShopAdvertInfoDao shopAdvertInfoDao;

    @Autowired
    private MessageServer messageServer;

    @Override
    public List<ShopAdvertInfoVO> getAdvertInfo(Long shopId) {
        if (shopId==null) {
            H2ShopBase shopBase = h2ShopBaseDao.getByUserId(SecurityUtils.getUserId());
            shopId=shopBase.getId();
        }
        List<H2ShopAdvertInfo> list=shopAdvertInfoDao.getByShopId(shopId,null);
        return BeanProUtils.copyListProperties(list,ShopAdvertInfoVO::new);
    }

    @Override
    public List<ShopAdvertInfoVO> getAdvertInfoByApp(Long shopId) {
        List<H2ShopAdvertInfo> list=shopAdvertInfoDao.getByShopId(shopId, Constants.YesNo.YES);
        return BeanProUtils.copyListProperties(list,ShopAdvertInfoVO::new);
    }

    @Override
    public R add(AddAdvert addAdvert) {
        if (addAdvert.getShopId()==null) {
            H2ShopBase shop = h2ShopBaseDao.getByUserId(SecurityUtils.getUserId());
            addAdvert.setShopId(shop.getId());
        }
        long count=shopAdvertInfoDao.countByShopId(addAdvert.getShopId());
        if (count>=10){
            return messageServer.failMsg(ExceptionEnum.QUANTITY_TOO_MAXIMUM);
        }

        H2ShopAdvertInfo info=BeanProUtils.copyProperties(addAdvert,H2ShopAdvertInfo::new);
        info.setShopId(addAdvert.getShopId());
        shopAdvertInfoDao.save(info);
        return R.ok();
    }

    @Override
    public R edit(EditAdvert editAdvert) {
        H2ShopAdvertInfo info=shopAdvertInfoDao.getById(editAdvert.getId());
        if (info==null){
            return messageServer.failMsg(ExceptionEnum.PARAM_IS_ERROR);
        }
        BeanProUtils.copyProperties(editAdvert,info);
        shopAdvertInfoDao.updateById(info);
        return R.ok();
    }

    @Override
    public R del(Long id) {
        shopAdvertInfoDao.removeById(id);
        return R.ok();
    }
}
